package com.hxkj.controller;

import com.hxkj.MQ.Sender.GeneratorStaticFileService;
import com.hxkj.domain.po.*;
import com.platform.common.domain.exception.CustomException;
import com.platform.common.domain.exception.CustomMessageException;
import com.platform.common.domain.result.ResultMessageStruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.Callable;

@Slf4j
@RestController
@RequestMapping(value = "/createFile")
public class GenFileController {

    @Autowired
    private GeneratorStaticFileService generatorStaticFileService;

    @RequestMapping("/genfile")
    public Callable<ResultMessageStruct> GenFile(@RequestBody String params) throws Exception {
        return () -> generatorStaticFileService.GenFile(params);
    }

    @RequestMapping("/genArticleFile")
    public void GenArticleFile(@RequestBody List<Article> list) throws Exception {
        generatorStaticFileService.GenArticleFile(list);
    }

    @RequestMapping("/genChannelFile")
    public void GenChannelFile(@RequestBody List<Channel> list) throws Exception {
        generatorStaticFileService.GenChannelFile(list);
    }

    @RequestMapping("/genColumnFile")
    public void GenColumnFile(@RequestBody List<Column> list) throws Exception {
        generatorStaticFileService.GenColumnFile(list);
    }

    @RequestMapping("/genLeafletFile")
    public ResultMessageStruct GenLeafletFile(@RequestBody Leaflet leaflet) {
        ResultMessageStruct resultMessageStruct = new ResultMessageStruct();
        try {
            generatorStaticFileService.GenLeafletFile(leaflet);
            resultMessageStruct.setCode(200);
            resultMessageStruct.setMessage("成功");
        } catch (Exception e) {
            e.printStackTrace();
            resultMessageStruct.setCode(400);
            resultMessageStruct.setMessage(e.getMessage());
        }
        return resultMessageStruct;
    }

    @RequestMapping("/genTagInfoFile")
    public void GenTagInfoFile(@RequestBody List<Tags> list) throws Exception {
        generatorStaticFileService.GenTagInfoFile(list);
    }

    @RequestMapping("/createSiteAll")
    public Callable<ResultMessageStruct> createSiteAll(@RequestBody FreeMarker freeMarker) {
        return () -> {
            ResultMessageStruct result = new ResultMessageStruct();
            try {
                generatorStaticFileService.createSiteAll(freeMarker);
                result.setCode(200);
                result.setMessage("成功");
            } catch (CustomException e) {
                result.setCode(e.getCode().getCode());
                result.setMessage(e.getMessage());
            } catch (CustomMessageException e) {
                result.setCode(e.getCode());
                result.setMessage(e.getMessage());
            } catch (Exception e) {
                log.error("生成全站静态文件错误", e);
                result.setCode(400);
                result.setMessage("生成全站静态文件错误");
            }
            return result;
        };
    }


    /**
     * 根据文章生成静态页面
     *
     * @param article
     * @throws Exception
     */
    @RequestMapping("/generateArticleHTML")
    public ResultMessageStruct generateArticleHTML(@RequestBody Article article) {
        ResultMessageStruct resultMessageStruct = new ResultMessageStruct();
        try {
            generatorStaticFileService.generateArticleHTML(article);
            resultMessageStruct.setCode(200);
            resultMessageStruct.setMessage("成功");
        } catch (Exception e) {
            e.printStackTrace();
            resultMessageStruct.setCode(400);
            resultMessageStruct.setMessage(e.getMessage());
        }
        return resultMessageStruct;
    }
}
